package com.ysxsoft.wdg.ui.activity.my.jf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.bean.AddressBean;
import com.ysxsoft.wdg.bean.response.JiFenExchangeResponse;
import com.ysxsoft.wdg.bean.response.MyAddressBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.address.AddressListActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class JfExchangeActivity extends Activity {

    @BindView(R.id.tv_attention)
    TextView tvAttention;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tele)
    TextView tvTele;
    @BindView(R.id.tv_dizhi)
    TextView tvDizhi;

    private MyAddressBean myAddressBean;
    private String productid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jf_exchange);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        myAddressBean = getIntent().getParcelableExtra("address");
        productid = getIntent().getStringExtra("productid");

        initView();
    }

    private void initView() {
        tvName.setText(myAddressBean.getName());
        tvTele.setText(myAddressBean.getPhone());
        tvDizhi.setText(myAddressBean.getProvince()+myAddressBean.getCity()+myAddressBean.getCounty()+myAddressBean.getAddress());
    }

    @OnClick({R.id.btn_select, R.id.btn_sure, R.id.rl_bg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_select:
                Intent i=new Intent(this,AddressListActivity.class);
                i.putExtra("code",1);
                startActivityForResult(i,AddressListActivity.ADDRESS_CHOOSE);
                break;
            case R.id.btn_sure:
                commit();
                break;
            case R.id.rl_bg:
                finish();
                break;
        }
    }

    private void commit(){
//        OkGo.<String>post(Urls.SCORE_exchange)
//                .tag(this)
//                .params("uid",myAddressBean.getUid())
//                .params("productid",productid)
//                .params("addressid",myAddressBean.getId())
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        KLog.e(response.body().toString());
//                        BaseBean baseBean = JsonUtil.parseJsonToBean(response.body().toString(),BaseBean.class);
//                        ToastUtil.showToast(baseBean.getMs());
//                        finish();
//                    }
//                });
            OkHttpUtils.post()
                    .url(UrlHost.FEN_EXCHANGE)
                    .addParams("uid", SharePrefUtils.getUserId())
                    .addParams("productid", productid)
                    .addParams("addressid",myAddressBean.getId())
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    if (!call.isCanceled()) {
                    }
                }

                @Override
                public void onResponse(String response, int id) {
                    if (response != null) {
                        JiFenExchangeResponse addressBean = JsonUtils.parseByGson(response, JiFenExchangeResponse.class);
                        if (addressBean == null) {
                            return;
                        }
                        if (CallbackCode.SUCCESS.equals(addressBean.getCode())) {
                            ToastUtils.showToast(addressBean.getMs());
                            finish();
                        }else{
                            ToastUtils.showToast(addressBean.getMs());
                        }
                    } else {
                    }
                }
            });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddressListActivity.ADDRESS_CHOOSE) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    AddressBean item = data.getParcelableExtra("address");
                    myAddressBean.setId(item.getId());
                    tvDizhi.setText(item.getAddress());
                    tvName.setText(item.getName());
                    tvTele.setText(item.getPhone());
                }
            }
        }
    }

    @Subscribe
    public void getEvent(MyAddressBean myAddressBean){
        this.myAddressBean = myAddressBean;
        initView();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

}
